package com.mtwp.dfs.rest.api.dfs;

import com.mtwp.dfs.domain.category.DiskStore;
import com.mtwp.dfs.service.category.DiskStoreService;
import com.twp.auth.annotation.MethodInfo;
import com.twp.common.component.process.exception.combo.IComboExecptionForTFunc;
import com.twp.common.tuple.ResultDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.security.Principal;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/fileDfs")
public class FastDfsController {

    @Autowired
    IComboExecptionForTFunc iComboExecptionForTFunc;

    @Autowired
    DiskStoreService diskStoreService;

    @MethodInfo(code = "fileDfsUpload")
    @PostMapping("/upload")
    ResultDTO upload(Principal principal, MultipartFile file, DiskStore diskStore){
        return iComboExecptionForTFunc.executeFlow(
                ResultDTO.builder().message("上传成功").build(),
                it-> {
                    ResultDTO resultDTO = diskStoreService.action(principal,diskStore.setFile(file));
                    it.setMessage(resultDTO.getMessage());
                    it.setIsSuccess(resultDTO.getIsSuccess());
                },
                (e,it)->{
                    it.setMessage("上传失败");
                    it.setIsSuccess(false);
                }
        );
    }

    @MethodInfo(code = "fileDfslogicDelete")
    @PostMapping("/logicDelete")
    ResultDTO logicDelete(@RequestBody List<Integer> ids){
        return iComboExecptionForTFunc.executeFlow(
                ResultDTO.builder().data("逻辑删除成功").build(),
                it-> {
                    diskStoreService.logicDelete(ids);
                },
                (e,it)->{
                    it.setMessage("逻辑删除失败");
                    it.setIsSuccess(false);
                }
        );
    }

    @MethodInfo(code = "fileDfsClear")
    @PostMapping("/clear")
    ResultDTO clear(@RequestBody List<Integer> ids){
        return iComboExecptionForTFunc.executeFlow(
                ResultDTO.builder().data("彻底删除存储成功").build(),
                it-> {
                    diskStoreService.clear(ids);
                },
                (e,it)->{
                    it.setMessage("彻底删除存储失败");
                    it.setIsSuccess(false);
                }
        );
    }
}
